#include "StdAfx.h"
#include "SApp.h"

SApp * SApp::m_pApp = NULL;

SApp::SApp()
{
	m_pManager = new SManager();
	AllocConsole(); 
	freopen("CONOUT$", "w+t", stdout);
}

SApp::~SApp(void)
{
	delete m_pManager;
}

SApp * SApp::GetApp()
{
	if (m_pApp == NULL)
	{
		m_pApp = new SApp;
	}
	return m_pApp;
}


void SApp::Add(SWin * pWin)
{
	m_pManager->Add(pWin);
}

void SApp::Init(HINSTANCE hIns)
{
	m_hIns = hIns;

	WNDCLASS wc = {0};
	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
	wc.hCursor = LoadCursor(NULL,IDC_ARROW);
	wc.hInstance = hIns;
	wc.lpfnWndProc = SManager::SinProc;
	wc.lpszClassName = "Sin";
	wc.style = CS_HREDRAW | CS_VREDRAW;
	RegisterClass(&wc);
}

UINT SApp::Run(void)
{
	MSG tMsg;
	while(GetMessage(&tMsg,NULL,0,0))
	{
		if (GetManager()->DealMessage(tMsg.hwnd, tMsg.message, tMsg.wParam, tMsg.lParam))
		{
			TranslateMessage(&tMsg);
			DispatchMessage(&tMsg);
		}
	}
	return tMsg.wParam;
}
